package br.com.ime.usp.cmep;


import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MapShow extends View{

	private Paint paint;
	private Bitmap bMap;
	private int xDraw;
	private int yDraw;
	private final int RADIUS = 14; //raio do circulo em torno do usuario
	private float proportion;
	private int xGPS;
	private int yGPS;
	private int textWidth;
	private int textHeight;
	private boolean circular1; //true para exibir o trajeto do circular1
	private boolean circular2; //true para exibir o trajeto do circular1
	private boolean bandejao; //true para exibir os bandejões
	private AssetManager assetManager;
	private String text;
	private Point p1[];
	private Point p2[];
	private Point bandex[];
	
	public MapShow(Context context, AttributeSet attrs) {
		super(context, attrs);
		if(bMap == null)
			createbMap();
		bMap = BitmapFactory.decodeResource(getResources(), R.drawable.map,null);
		if(paint == null)
			paint = new Paint();
		xGPS = (int)(-2*RADIUS*proportion);
		yGPS = (int)(-2*RADIUS*proportion);
		bandejao = false;
		circular1 = false;
		circular2 = false;
	}
		
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void changeBandejao(){
		bandejao = !bandejao;
	}
	
	public void changeCircular1(){
		circular1 = !circular1;
	}

	public void changeCircular2(){
		circular2 = !circular2;
	}

	public int getTextWidth() {
		return textWidth;
	}

	public void setTextWidth(int textWidth) {
		this.textWidth = textWidth;
	}

	public int getTextHeight() {
		return textHeight;
	}

	public void setTextHeight(int textHeight) {
		this.textHeight = textHeight;
	}
	
	public int getxGPS() {
		return xGPS;
	}

	public void setxGPS(int xGPS) {
		this.xGPS = xGPS;
	}

	public int getyGPS() {
		return yGPS;
	}

	public void setyGPS(int yGPS) {
		this.yGPS = yGPS;
	}

	public int getxDraw() {
		return xDraw;
	}

	public void setxDraw(int xDraw) {
		this.xDraw = xDraw;
	}

	public int getyDraw() {
		return yDraw;
	}

	public void setyDraw(int yDraw) {
		this.yDraw = yDraw;
	}

	public Bitmap getbMap() {
		return bMap;
	}

	public void setbMap(Bitmap bMap) {
		this.bMap = bMap;
	}

	public double getProportion() {
		return proportion;
	}

	public void setProportion(float proportion) {
		this.proportion = proportion;
	}
	
	public AssetManager getAssetManager() {
		return assetManager;
	}

	public void setAssetManager(AssetManager assetManager) {
		this.assetManager = assetManager;
	}
	
		public void recycleMap()
	{
		bMap.recycle();
	}

	public void createbMap(){
		bMap = BitmapFactory.decodeResource(getResources(), R.drawable.map,null);
	}
	
	public void setp1(Point p[]){
		this.p1 = p;
	}
	
	public void setp2(Point p[]){
		this.p2 = p;
	}

	public Point[] getBandex() {
		return bandex;
	}

	public void setBandex(Point bandex[]) {
		this.bandex = bandex;
	}
	
	// desenha
	protected void onDraw(Canvas canvas) {
		if(bMap != null){
			paint.setStyle(Paint.Style.STROKE);
			canvas.drawBitmap(bMap, xDraw, yDraw, paint);
			if(circular1 && p1 != null){
				paint.setStrokeWidth(2*proportion);
				paint.setColor(Color.RED);
				for(int i = 0; p1[i+1] != null; i++){
					canvas.drawLine(p1[i].getX()*proportion + xDraw, p1[i].getY()*proportion + yDraw, p1[i+1].getX()*proportion + xDraw, p1[i+1].getY()*proportion + yDraw, paint);
				}
			}
			if(circular2 && p2 != null){
				paint.setStrokeWidth(2*proportion);
				paint.setColor(Color.BLUE);
				for(int i = 0; p2[i+1] != null; i++){			
					canvas.drawLine(p2[i].getX()*proportion + xDraw, p2[i].getY()*proportion + yDraw, p2[i+1].getX()*proportion + xDraw, p2[i+1].getY()*proportion + yDraw, paint);
				}
			}
			if(bandejao && bandex!=null){
				paint.setStyle(Paint.Style.FILL);
				for(int i = 0; bandex[i] != null; i++){
					paint.setColor(Color.DKGRAY);	
					canvas.drawCircle(bandex[i].getX()*proportion + xDraw, bandex[i].getY()*proportion+yDraw, RADIUS*proportion, paint);
					paint.setStrokeWidth(proportion);
					paint.setColor(Color.WHITE);		
					paint.setTextSize((10*proportion));
					canvas.drawText(bandex[i].getText(), bandex[i].getX()*proportion + xDraw-(RADIUS-1)*proportion, bandex[i].getY()*proportion+yDraw, paint);
				}
			}
			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(Color.BLACK);
			paint.setStrokeWidth(proportion);
			canvas.drawCircle(xGPS + xDraw, yGPS + yDraw, (RADIUS*proportion), paint);
			paint.setStyle(Paint.Style.FILL);
			paint.setStrokeWidth(1);
			paint.setColor(Color.BLACK);
			paint.setTextSize((10*proportion));
			canvas.drawText("© OpenStreetMap contributors, CC BY-SA", 5, textHeight - (int)(10*proportion), paint);
		}
	}

	// chamado pela thread
	public void update(int xDraw, int yDraw) {
		this.xDraw = xDraw;
		this.yDraw = yDraw;
	}

}
